package com.sky.cqculture.Contraller;

import com.sky.cqculture.pojo.Comment;
import com.sky.cqculture.pojo.Result;
import com.sky.cqculture.service.CommentService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.HashMap;  // 添加缺失的导入语句
import java.util.Map;

@RestController
@RequestMapping("/api/comments")
public class CommentController {
    
    @Autowired
    private CommentService commentService;
    
    /**
     * 添加评论
     * POST /api/comments
     */
    @PostMapping
    public Result addComment(@RequestBody Map<String, Object> commentInfo) {
        try {
            // 验证参数 - 只处理必要的字段
            Integer heritageId = (Integer) commentInfo.get("heritageId");
            String content = (String) commentInfo.get("content");
            Integer userId = (Integer) commentInfo.get("userId");  // 获取用户ID
            
            if (heritageId == null) {
                return Result.error("非遗项目ID不能为空");
            }
            if (content == null || content.trim().isEmpty()) {
                return Result.error("评论内容不能为空");
            }
            if (userId == null) {
                return Result.error("用户ID不能为空");  // 确保用户ID不为空
            }
            
            // 创建评论对象 - 仅设置必要字段
            Comment comment = new Comment();
            comment.setUserId(userId);  // 设置用户ID
            comment.setHeritageId(heritageId);
            comment.setContent(content);
            comment.setCreateTime(LocalDateTime.now());
            
            // 添加评论
            commentService.addComment(comment);
            
            // 构建返回结果 - 使用 HashMap 代替 Map.of()，因为 HashMap 允许值为 null
            Map<String, Object> result = new HashMap<>();
            result.put("id", comment.getId());
            result.put("userId", comment.getUserId());
            result.put("heritageId", heritageId);
            result.put("content", content);
            result.put("createTime", comment.getCreateTime());
            
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("评论失败，请重试");
        }
    }
    
    /**
     * 获取指定非遗项目的评论列表
     * GET /api/comments/heritage/{heritageId}
     */
    @GetMapping("/heritage/{heritageId}")
    public Result getCommentsByHeritageId(@PathVariable Integer heritageId) {
        try {
            if (heritageId == null) {
                return Result.error("非遗项目ID不能为空");
            }
            
            List<Comment> comments = commentService.findCommentsByHeritageId(heritageId);
            return Result.success(comments);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取评论列表失败");
        }
    }
    
    /**
     * 删除评论
     * DELETE /api/comments/{id}
     */
    @DeleteMapping("/{id}")
    public Result deleteComment(@PathVariable Integer id) {
        try {
            if (id == null) {
                return Result.error("评论ID不能为空");
            }
            
            // 这里可以根据实际需求添加权限验证逻辑
            // 由于前端已做验证，这里直接删除
            commentService.deleteComment(id);
            
            return Result.success("评论删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("评论删除失败");
        }
    }
}